$(function(){
	var form = $("#form_add_role").Validform({
		tiptype:2, //可选项 1=>pop box,2=>side tip(parent.next.find; with default pop),3=>side tip(siblings; with default pop),4=>side tip(siblings; none pop)，默认为1，也可以传入一个function函数，自定义提示信息的显示方式（可以实现你想要的任何效果，具体参见demo页）;
		showAllError:false,//可选项 true | false，true：提交表单时所有错误提示信息都会显示，false：一碰到验证不通过的就停止检测后面的元素，只显示该元素的错误信息;
		postonce:true, //可选项 表单是否只能提交一次，true开启，不填则默认关闭;
		ajaxPost:true, //使用ajax方式提交表单数据，默认false，提交地址就是action指定地址;
		datatype:{//传入自定义datatype类型，可以是正则，也可以是函数（函数内会传入一个参数）;
			"z2-4" : /^[\u4E00-\u9FA5\uf900-\ufa2d]{2,4}$/,
			"*1-10":/^.{1,40}$/,
			"*0-20":/^.{0,20}$/
		},
		callback:function(data){
			if(data.status=="T"){
				$("#formsearch").submit();
			}else{
				form.resetStatus();
			}
		}
	});
	form.addRule([
		{
			ele:"#role-roleName",
			datatype:"*1-10",
			errormsg:"最大长度10个字符"
		},
		{
			ele:"#role-remark",
			datatype:"*0-20",
			errormsg:"最大长度20个字符",
			ignore:"ignore"
		}
	]);
	/**
	 * 添加修改角色
	 * @param flag
	 * @param _this
     */
	function add_edit_role(flag,_this){
		$.ajax({
			url:htx+"/role/getRoleList",
			type:"POST",
			data:"",
			dataType:"JSON",
			success:function(data){
				var s = $('#role-parent-role-id');
				s.empty();
				$('<option />', {value: "", text: "-请选择-"}).appendTo(s);
				for(var i=0;i<data.length;i++) {
					$('<option />', {value: data[i].id, text: data[i].roleName}).appendTo(s);
				}
			}
		});
		if(flag=="add"){
			admin.dialog_show("添加角色","#myModal","#add_role",400,250);
			form.resetForm();
		}else{
			var id = $(_this).attr("data-roleid");
			$.ajax({
				url:htx+"/role/selectRoleById",
				data:{"id":id},
				dataType:"JSON",
				success:function(data){
					if(data.status=="T"){
						$("#role-id").val(data.info.id);
						$("#role-roleName").val(data.info.roleName);
						$("#role-remark").val(data.info.remark);
						$("#role-parent-role-id").val(data.info.parentRoleId);
						admin.dialog_show("修改角色","#myModal","#add_role",400,250);
					}else{
						alert(data.info);
					}
				}
			});
		}
		
	}
	$("#roleAdd").click(function(){
		add_edit_role("add",this);
	});
	$("a[name=role_edit]").click(function(){
		add_edit_role("edit",this);
	});
	$("a[name=role_del]").click(function(){
		var _this = this;
		layer.confirm("您确定要删除此角色？删除后此角色下的所有账号将无法登陆！", {
		    btn: ["确定","取消"] //按钮
		}, 
		//确定btn
		function(){
			var index = layer.load();
			var id = $(_this).attr("data-roleid");
			$.ajax({
				url:htx+"/role/deleteRole",
				data:{"id":id},
				dataType:"JSON",
				success:function(data){
					layer.close(index);
					if(data.status=="T"){
						layer.msg(data.info,{time:1000},function(){$("#formsearch").submit();})
					}else{
						layer.msg(data.info)
					}
				}
			});
		}, 
		//取消btn
		function(){
		    
		});
	});
	/**
	 * 授权按钮
	 */
	$("a[name=role_auth]").click(function(){
        var roleid = $(this).attr("data-roleid");
        $("#auth_form").find("#roleid").val(roleid);
		//初始化树
		var zNodes =new Array();
		$.ajax({
			url:htx+"/menu/getMenuToJSON",
			async: false,
			dataType:"JSON",
			success:function(data) {
				if (data.status == "T") {
					var info = data.info;
					for (var i = 0; i < info.length; i++) {
						var json = {id: info[i].id, pId: info[i].parentMenuId, name: info[i].cname};
						zNodes.push(json);
					}
				}else{
					layer.msg(data.info);
				}
			}
		});
		//初始化菜单树参数
		var setting = {
			view: {
				dblClickExpand: true,
				showLine: false,
				selectedMulti: false,
				showIcon: true
			},
			data: {
				simpleData: {
					enable:true,
					idKey: "id",
					pIdKey: "pId",
					rootPId: ""
				}
			},
			check:{
				enable:true,
				chkStyle: "checkbox",
				chkboxType: { "Y": "ps", "N": "ps" }
			}
		};
		admin.show_ztree(zNodes,"role-tree",true,false,setting);
		//反显此资源配置的角色树
		$.ajax({
			url: htx + "/role/selectRoleAuth",
			type: "POST",
			data: {roleId: roleid},
			dataType: "JSON",
			success: function (data) {
				if(data.status=="T"){
					var info = data.info;
					var zTree = $.fn.zTree.getZTreeObj("role-tree");
					for (var i = 0; i < info.length; i++) {
						var nodes = zTree.getNodesByParam("id", info[i].menuId, null);
						zTree.checkNode(nodes[0], true, false);
					}
				}else{
					layer.msg(data.info);
				}
			}
		});
		admin.dialog_show("授权","#myModal","#auth_form",400,300);
	});

	/**
	 * 权限保存按钮
	 */
	$("#save_auth_btn").click(function(){
		var zTree = $.fn.zTree.getZTreeObj("role-tree");
		var check=zTree.getCheckedNodes();
		var childNodeArray = new Array();
		for(var i=0;i<check.length;i++){
			childNodeArray.push(check[i].id);
		}
		layer.confirm("您确定要修改当前资源的权限控制？", {
				btn: ["确定","取消"] //按钮
			},
			//确定btn
			function(){
				var index = layer.load();
				var roleid = $("#auth_form input[name=roleid]").val();
				$.ajax({
					url:htx+"/role/saveRoleAuth",
					type:"POST",
					data:{"roleid":roleid,"menuArray":childNodeArray},
					dataType:"JSON",
					success:function(data){
						layer.close(index);
						if(data.status=="T"){
							layer.msg(data.info,{time:1000},function(){$("#formsearch").submit();});
						}else{
							layer.msg(data.info);
						}
					}
				});
			},
			//取消btn
			function(){

			});
	});
});
